using System.Configuration;
using System.Text;

namespace Framework.Core.Io
{
    public class ConfigurationManagerAdapter : IConfigurationManagerAdapter
    {
        public TInterface GetSection<TInterface, TConfigSection>()
            where TConfigSection : TInterface
            where TInterface : class
        {
            return ConfigurationManager.GetSection(GetSectionName(typeof (TConfigSection).Name)) as TInterface;
        }

        public string GetConnectionString(string key)
        {
            return ConfigurationManager.ConnectionStrings[key].ConnectionString;
        }

        public static string GetSectionName(string s)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append(s.Substring(0, 1).ToLower());
            stringBuilder.Append(s.Substring(1, s.Length - 1));
            return stringBuilder.ToString();
        }
    }
}